<template>
  <div class="content">
    <el-form
      ref="ruleFormRef"
      style="max-width: 800px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="商品类型" prop="name">
        <div class="list-card">
          <div class="item on">
            <div class="title">普通商品</div>
            <div class="name">(物流发货)</div>
          </div>
          <div class="item">
            <div class="title">虚拟商品</div>
            <div class="name">(虚拟发货)</div>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-button type="primary" @click="submitForm(ruleFormRef)">选择库存商品</el-button>
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <div class="form-item-line">
        <el-form-item label="供应商" prop="region">
          <el-select class="select" v-model="ruleForm.region" placeholder="选择供应商">
            <el-option label="贵阳有限公司" value="shanghai" />
            <el-option label="北京有限公司" value="beijing" />
          </el-select>
        </el-form-item>
        <el-form-item label="生产企业" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
      </div>

      <el-form-item label="商品分类" prop="region">
        <el-select class="select" v-model="ruleForm.region" placeholder="选择供应商">
          <el-option label="贵阳有限公司" value="shanghai" />
          <el-option label="北京有限公司" value="beijing" />
        </el-select>
      </el-form-item>

      <el-form-item label="商品轮播图" prop="count">
        <com-Upload-img></com-Upload-img>
      </el-form-item>
      <el-form-item label="添加视频" prop="delivery">
        <el-switch v-model="ruleForm.delivery" />
      </el-form-item>
      <el-form-item label="视频方式" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio value="Sponsorship">传视频</el-radio>
          <el-radio value="Venue">视频地址</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="视频" prop="count">
        <com-Upload-img></com-Upload-img>
      </el-form-item>
      <el-form-item label="备注" prop="desc">
        <el-input v-model="ruleForm.desc" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'

interface RuleForm {
  name: string
  region: string
  count: string
  date1: string
  date2: string
  delivery: boolean
  location: string
  type: string[]
  resource: string
  desc: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: 'Hello',
  region: '',
  count: '',
  date1: '',
  date2: '',
  delivery: false,
  location: '',
  type: [],
  resource: '',
  desc: ''
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' }
  ],
  region: [
    {
      required: true,
      message: 'Please select Activity zone',
      trigger: 'change'
    }
  ],
  count: [
    {
      required: true,
      message: 'Please select Activity count',
      trigger: 'change'
    }
  ],
  date1: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a date',
      trigger: 'change'
    }
  ],
  date2: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a time',
      trigger: 'change'
    }
  ],
  location: [
    {
      required: true,
      message: 'Please select a location',
      trigger: 'change'
    }
  ],
  type: [
    {
      type: 'array',
      required: true,
      message: 'Please select at least one activity type',
      trigger: 'change'
    }
  ],
  resource: [
    {
      required: true,
      message: 'Please select activity resource',
      trigger: 'change'
    }
  ],
  desc: [{ required: true, message: 'Please input activity form', trigger: 'blur' }]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`
}))
</script>

<style lang="scss" scoped>
.form-item-line {
  display: flex;
  justify-content: space-between;
  .item {
    padding-left: 30px;
  }
  .select {
    width: 300px;
  }
  .lable {
    font-size: 12px;
  }
}

.list-card {
  display: flex;
  padding: 0;
  .item {
    border: 1px solid #eee;
    border-radius: 5px;
    padding: 20px 50px;
    margin-right: 10px;
    position: relative;
    cursor: pointer;
    transition: 0.3 initial;
    &.on {
      border: 1px solid #409eff;
      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        right: 0;
        width: 0;
        height: 0;
        border-bottom: 26px solid #409eff;
        border-left: 26px solid transparent;
        z-index: 8;
      }
      &::before {
        content: '✔';
        position: absolute;
        bottom: 5px;
        right: -7px;
        color: #fff;
        width: 20px;
        height: 20px;
        display: flex;
        text-align: center;
        z-index: 9;
      }
    }
    .title {
      font-size: 16px;
      font-weight: bold;
      padding-bottom: 0px;
      line-height: 12px;
      padding-bottom: 10px;
    }
    .name {
      font-size: 12px;
      color: #ccc;
      line-height: 12px;
    }
  }
}
</style>
